﻿using CrystalDecisions.CrystalReports.Engine;
using DSPL.Contracts.Interfaces;
using DSPL.Reports.Classes.Category;
using System;
using System.Collections.Generic;
using System.Data;

namespace DSPL.Reports.Classes
{
    public class ReportsDocument 
    {
        #region Properties
        /// <summary>
        /// Set the report ID by seperating ':' eg: Cat1:Report1
        /// Ecole_Constant.Report_Id_Seperator
        /// </summary>
        public string ReportID { get; set; }

        /// <summary>
        /// Add the parameter id and values using InputValueStruct
        /// </summary>        
        public List<IReportParameters> InputValueList
        {
            get { return inputValueList; }
            set
            {
                if (value == null)
                    InputValueList = new List<IReportParameters>();
                else
                    inputValueList = value;
            }
        }
        
        #endregion

        #region private variables

        private List<IReportParameters> inputValueList;
        //private List<IReportParameters> inputValueList;

        #endregion

        #region constructor
        public ReportsDocument()
        {

        }
        #endregion

        public ReportDocument GetReportDocument(string reportId,DataTable reportDataTable= null)
        {
            ReportID = reportId;
            StockMasterReports mstStockReports = null;
            
            try
            {
                string[] categoryId = ReportID.Split(new string[]{":"}, StringSplitOptions.RemoveEmptyEntries);
                string catCode = categoryId[0];
                string repCode = categoryId[1];

                switch (catCode)
                {
                    case "Stock.Master":
                        mstStockReports = new StockMasterReports(repCode);
                        mstStockReports.ParameterValueList = InputValueList;
                        break;

                    default:
                        
                        throw new Exception("Invalid category Code: " + catCode + "\n@ " + this.ToString());
                }
                
                if (mstStockReports != null)
                {
                    return mstStockReports.GetReportDocument(reportDataTable);
                }

                throw new Exception("Can not create a report " + "\n@ " + this.ToString());
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                //if (mstStockReports != null)
                //{
                //    mstStockReports.Dispose();
                //}
            }


        }



        
    }
}
